package com.bitauto.i.commonUtils.arithmetic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class InvitationService {
    private Map<String, List<String>> invitations = new HashMap<>();
    private int rewardThreshold= 5;

    public void processInvitation(String inviter, String invitee){
        List<String> invites = invitations.get(inviter);
        if (invites == null){
            invites = new ArrayList<>();
            invitations.put(inviter, invites);
        }
        invites.add(invitee);
        if (invites.size() >= rewardThreshold) {
            sendReward(inviter);
            invitations.remove(inviter);
        }
    }
    private void sendReward(String inviter) {
        //模拟数据库操作（假设此处可能抛出异常）
        System.out.println("Reward sent to: " + inviter);
    }
    public static void main(String[] args) {
        InvitationService service = new InvitationService();
        //模拟井发邀请
        for (int i=0; i <10 ;i++ ) {
            new Thread(() -> {
                service.processInvitation("userA", "userB");
            }).start();
        }
    }
}
